<template>
  <div class="upload-img">
    <i class="el-icon-circle-close" @click="closeImg"></i>
    <span class="upload-img-text">上传图片</span>

    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :http-request="upload"
    >
      <img v-if="value" :src="value" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon" />
      <el-progress
        v-show="showProgress"
        type="circle"
        :percentage="progress"
        class="progress"
      />
    </el-upload>
  </div>
</template>

<script>
const COS = require('cos-js-sdk-v5')
const cos = new COS({
  SecretId: 'AKIDh9Xmrn96mIalkyD57za2JYN2X0JZw9hI',
  SecretKey: 'gFvc9WgabMFsHRK1RBRSDjythK7fx7D3'
})

export default {
  props: {
    value: {
      type: String,
      default: ''
    },
    index: {
      type: Number
    }
  },
  data () {
    return {
      progress: 0,
      showProgress: false
    }
  },
  methods: {
    upload (res) {
      if (!res.file) return
      this.showProgress = true
      cos.putObject(
        {
          Bucket: 'gg-1305554343' /* 必须 */,
          Region: 'ap-beijing' /* 存储桶所在地域，必须字段 */,
          Key: res.file.name /* 必须 */,
          StorageClass: 'STANDARD',
          Body: res.file, // 上传文件对象
          onProgress: (progressData) => {
            console.log(progressData)
            console.log(JSON.stringify(progressData))
            this.progress = Math.ceil(progressData.percent * 100)
          }
        },
        (err, data) => {
          console.log('上传存储桶成功', err || data)
          if (data.statusCode === 200) {
            this.$emit('input', `https:${data.Location}`)
            this.showProgress = false
          }
        }
      )
    },
    handleAvatarSuccess (res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload (file) {
      const isPNG = file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isPNG) {
        this.$message.error('上传头像图片只能是 PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isPNG && isLt2M
    },
    // 关闭图片
    closeImg () {
      this.$emit('closeImg', this.index)
    }
  }
}
</script>

<style scoped lang="scss">
.avatar-uploader ::v-deep .avatar-uploader-icon {
  width: 100px;
  height: 60px;
  // height: 70px;

}
.avatar-uploader {
  // height: 70px;
  height: 80px;
}
.avatar-uploader ::v-deep .el-upload {
  width: 100px;
  height: 60px;
  // height: 70px;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.progress {
  position: absolute;
  display: flex;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: transparent;
}
.upload-img {
  position: relative;
  display: inline-block;
}
.upload-img-text {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 58px;
  height: 25px;
  line-height: 25px;
  transform: translate(-50%,-50%);
}
/* 关闭图片 */
.el-icon-circle-close {
  position: absolute;
  right: -9px;
  top: 2px;
  font-size: 20px;
  cursor: pointer;
  z-index: 10;
}
</style>
